package de.gameduell.framework.gui.animation {
	import fl.transitions.Tween;
	import fl.transitions.TweenEvent;

	import de.gameduell.framework.gui.animation.Tweenable;

	import flash.events.EventDispatcher;	

	/**
	 * @author cbra
	 */
//	public class GDTween extends GDFastTween implements Tweenable {
	public class GDTween extends EventDispatcher implements Tweenable {

		private var parameters:Array;
		private var tween:Tween;

//		public function GDTween(aTweenedObject:Object,aProperty:String,aEaseType:Function, aStartValue:Number, aEndValue:Number, aDuration:Number, aUseSeconds:Boolean) {
//			super(aTweenedObject,aProperty,aEaseType,aStartValue,aEndValue,aDuration,aUseSeconds);
//		}


		/**
		 * callback which will be called for every tick
		 * 
		 * @param tweenedObject  the object that will be tweened
		 * @param property		 the property that will be tweened
		 * @param EaseType		 easytype for the tween
		 * @param startValue     the value from wich the animation starts
		 * @param endValue		 the value where the animation finishs
		 * @param useSeconds	 flag to asign if the animation is timebased or framebased
		 */
		public function GDTween(tweenedObject:Object,property:String,EaseType:Function, startValue:Number, endValue:Number, duration:Number, useSeconds:Boolean) {
			parameters = new Array();
			parameters.push(tweenedObject);
			parameters.push(property);
			parameters.push(EaseType);
			parameters.push(startValue);
			parameters.push(endValue);
			parameters.push(duration);
			parameters.push(useSeconds);
			TweenManager.getInstance().add(this);
		}

		/**
		 * starts the tween
		 */
		public function start():void {
			if(tween == null) {
				tween = new Tween(parameters[0], parameters[1], parameters[2], parameters[3], parameters[4], parameters[5], parameters[6]);
				tween.addEventListener(TweenEvent.MOTION_FINISH, onMotionEvent);
				tween.addEventListener(TweenEvent.MOTION_CHANGE, onMotionEvent);
				tween.addEventListener(TweenEvent.MOTION_LOOP, onMotionEvent);
				tween.addEventListener(TweenEvent.MOTION_RESUME, onMotionEvent);
				tween.addEventListener(TweenEvent.MOTION_START, onMotionEvent);
				tween.addEventListener(TweenEvent.MOTION_STOP, onMotionEvent);
			} else {
				tween.start();
			}
		}

		private function onMotionEvent(event:TweenEvent):void {
			dispatchEvent(new TweenEvent(event.type, event.time, event.position,event.bubbles,event.cancelable));
		}

		public function get isPlaying():Boolean {
			return tween != null && tween.isPlaying;
		}

		public function stop():void {
			if (tween != null) {
				tween.stop();
			}
		}

		public function get time():Number {
			return tween.time;
		}

		public function set finish(endValue:Number):void {
			tween.finish = endValue;	
		}
		
		public function continueTo(finish:Number, duration:Number):void{
			tween.continueTo(finish,duration);
		}
		
		public function get obj():Object{
			return parameters[0];
		} 
	}
}
